package stack;

public class MyStack {
    private String[] data;  //存储元素的数组
    private int size = 0;   //有效元素个数
    //无参构造函数
    public MyStack(){
        this(100);
    }
    //构造能容量大小为capacity的栈
    public MyStack(int capacity) {
        this.data = new String[capacity];
    }
    //扩容
    private void resize(){
        String[] newData = new String[data.length + (data.length >> 1)];
        for(int i = 0; i < size; i++){
            newData[i] = data[i];
        }
        data = newData;
    }
    //入栈
    public void push(String elem){
        if(isFull()){
            resize();
        }
        data[size++] = elem;
    }
    //出栈
    public String pop(){
        if(isEmpty()){
            return null;
        }
        String ret = data[size - 1];
        size--;
        return ret;
    }
    //获取栈顶元素
    public String peek(){
        if(isEmpty()){
            return null;
        }
        return data[size - 1];
    }
    //获取有效元素个数
    public int size(){
        return size;
    }
    //判空
    public boolean isEmpty(){
        return size == 0;
    }
    //判满
    public boolean isFull(){
        return size == data.length;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack(10);
        for(int i = 0; i < 3; i++){
            stack.push("" + (char)('a' + i));
        }

        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.size);
        System.out.println(stack.isEmpty());
        System.out.println(stack.isFull());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.size);
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek());

    }
}

























